package com.llt.bottomnavigationf.fraction;


import com.llt.bottomnavigationf.util.LogUtil;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import java.util.List;

/**
 * fraction 工具类 提供tabber 页面的显示隐藏以及初始化
 */
public class FractionUtil {
    private Integer currentSaveIndex=0;//当前正在显示的索引值
    private List<Fraction> fractions ;//小碎片数组 即tabbar页面

    private FractionAbility ability;
    public FractionUtil(List<Fraction> fractions,Integer currentSaveIndex,FractionAbility ability,int resourceTableId) {
        if (fractions == null || fractions.size() == 0) {
            throw new NullPointerException("fractions 必须有值");//
        }

        this.fractions = fractions;
        this.ability=ability;
        this.currentSaveIndex =currentSaveIndex;
        initTabberPage(resourceTableId);
    }
    public FractionUtil initTabberPage(int resourceTableId){

        if (this.currentSaveIndex < 0 || this.currentSaveIndex >= fractions.size()) {
            throw new NullPointerException("初始化索引值是非法的");
        }
        FractionScheduler scheduler = getFractionManager().startFractionScheduler();

        for (Fraction fraction : fractions) {
            scheduler.add(resourceTableId, fraction)
                    .hide(fraction);
        }

        scheduler.show(fractions.get(currentSaveIndex)).submit();
        return this;
    }
    /**
     * 获取Fraction管理器对象
     @return
     */
    public FractionManager getFractionManager(){

        if(this.ability instanceof FractionAbility){

            return this.ability.getFractionManager();
        }
        return null;
    }
    /**
     *
     * @param indexshow 显示点击的页面索引
     */
    public void show(int indexshow){
        if (indexshow >= 0 && indexshow < fractions.size()) {
            getFractionManager()
                    .startFractionScheduler()
                    .hide(this.fractions.get(this.currentSaveIndex))
                    .show(this.fractions.get(indexshow))
                    .submit();

            this.currentSaveIndex= indexshow;
        }

    }
}
